package com.eye.catcher.controller;


import com.alibaba.fastjson.JSONObject;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TYwDaBaseInfo;
import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.param.ResponseData;
import com.eye.catcher.param.ResponseUtil;
import com.eye.catcher.rabbit.MQDto;
import com.eye.catcher.service.generate.ITYwDaBaseInfoService;
import com.eye.catcher.system.yw.Info.param.BaseInfoDto;
import com.eye.catcher.system.yw.Info.param.BaseInfoPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 基地表 前端控制器
 * </p>
 *
 * @author huzhiwei
 * @since 2022-05-12
 */
@Slf4j
@RestController
@RequestMapping("/eyeCatcher-system/base-info")
public class TYwDaBaseInfoController extends TSysDaBaseController {
    private final static String MODULE_CODE = "/base-info";
    private final static String MODULE_NAME = "基地";

    @Autowired
    private ITYwDaBaseInfoService infoService;

    @PostMapping("/addBaseInfo")
    @ResponseBody
    public ResponseData addBaseInfo(@RequestBody TYwDaBaseInfo baseInfo) {
        String methodCode = "/addBaseInfo";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,baseInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            baseInfo.setCreateUserId(userId);
            this.infoService.addBaseInfo(baseInfo);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    @PostMapping("/editBaseInfo")
    @ResponseBody
    public ResponseData editBaseInfo(@RequestBody TYwDaBaseInfo baseInfo) {
        String methodCode = "/editBaseInfo";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,baseInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            baseInfo.setUpdateUserId(userId);
            this.infoService.editBaseInfo(baseInfo);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * @author huzhiwei
     * @param ids 项目id  多个id  ， 分割
     * @return
     */
    @PostMapping("/deleteBaseByIds")
    @ResponseBody
    public ResponseData deleteBaseByIds(@RequestParam(value = "ids") String ids) {
        String methodCode = "/deleteBaseByIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_DELETE,"ids: "+ids,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.infoService.deleteByIds(ids);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    @PostMapping("/getAllList")
    @ResponseBody
    public ResponseData getAllList(@RequestBody BaseInfoDto baseInfo) {
        String methodCode = "/getAllList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,baseInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            baseInfo.setUserId(userId);
            commonResponse = CommonResponse.success();
            commonResponse.setData(this.infoService.getAllList(baseInfo));
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    @PostMapping("/getPageList")
    @ResponseBody
    public ResponseData getPageList(@RequestBody BaseInfoDto baseInfo) {
        String methodCode = "/getPageList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,baseInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            baseInfo.setUserId(userId);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.infoService.getPageList(
                            BaseInfoPage.builder()
                                    .baseInfo(baseInfo)
                                    .page(this.getPageInfo(baseInfo))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * 导出模板
     */
    @RequestMapping(path = "/exportBaseTemplate")
    public @ResponseBody
    ResponseData exportBaseTemplate(HttpServletRequest request, HttpServletResponse response) {
        String methodCode = "/exportBaseTemplate";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode, getLoginUserId(), MODULE_CODE, MODULE_NAME, getClass(),
                    SbCode.OPERATION_DOWLOAD, null, null, null);
            this.sendInitRabbitMqLog(mqDto);
            String msg = this.infoService.exportBaseTemplate(request,response);
            if (StringUtils.isNotEmpty(msg)) {
                commonResponse = CommonResponse.getInstance();
                commonResponse.setCode(SbCode.SYSTEM_ERROR.getCode());
                commonResponse.setMsg(msg);
                commonResponse.setData(null);
            } else {
                commonResponse = CommonResponse.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * 导入
     */
    @RequestMapping(path = "/uploadBaseTemplate")
    public @ResponseBody
    ResponseData uploadBaseTemplate(@RequestParam("file") MultipartFile file) {
        String methodCode = "/uploadBaseTemplate";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_UPLOAD,null,null,null);
            this.sendInitRabbitMqLog(mqDto);
            String msg = this.infoService.uploadBaseTemplate(file,userId);
            if (StringUtils.isNotEmpty(msg)) {
                commonResponse = CommonResponse.getInstance();
                commonResponse.setCode(SbCode.SYSTEM_ERROR.getCode());
                commonResponse.setMsg(msg);
                commonResponse.setData(null);
            } else {
                commonResponse = CommonResponse.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

}
